﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mediation
{

    #region 第一个例子 
    /*
        场景是剪切板的使  未完善中介类逻辑代码
     */

    public enum NotifyType
    {
        Null,
        OnChange,
        Click,
    }

    //中介者抽象
    public abstract class Mediator
    {
        protected List<Element> elementList = new List<Element>();
        public abstract void Notify(NotifyType notify);
        public void AddElement(Element element)
        {
            this.elementList.Add(element);
        }
    }

    //抽象元素
    public abstract class Element
    {
        Mediator mediator;
        public Element(Mediator mediator)
        {
            this.mediator = mediator;
            mediator.AddElement(this);
        }
        public virtual void NotifyMediator(NotifyType notifyInfo)
        {
            mediator.Notify(notifyInfo);
        }

    }

    //剪切按钮
    public class CutMenuItem : Element
    {
        public CutMenuItem(Mediator mediator) : base(mediator)
        {
        }

        public void ClickCut()
        {
            NotifyMediator(NotifyType.Click);
        }

    }

    //文本区域
    public class TextArea : Element
    {
        private string text;

        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        public TextArea(Mediator mediator) : base(mediator)
        {

        }

        public void OnChange()
        {
            NotifyMediator(NotifyType.OnChange);
        }
    }

    //剪切板
    public class ClipBoard : Element
    {
        private string curtText;
        public string CurtText
        {
            get { return curtText; }
            set { curtText = value; }
        }

        public ClipBoard(Mediator mediator) : base(mediator)
        {
        }
        public void OnChange()
        {
            NotifyMediator(NotifyType.OnChange);
        }
    }

    //工具条按钮
    public class ToolBarButton : Element
    {
        public ToolBarButton(Mediator mediator) : base(mediator)
        {
        }
    }

    //中介者具体实现 
    public class MediatorClass : Mediator
    {
        public override void Notify(NotifyType notify)
        {
            switch (notify)
            {
                case NotifyType.Null:
                    break;
                case NotifyType.OnChange:
                    break;
                case NotifyType.Click:
                    string temText = string.Empty;
                    foreach (var item in this.elementList)
                    {
                        if (item is TextArea )
                        {
                            temText = ((TextArea)item).Text;
                            ((TextArea)item).Text = string.Empty;
                        }
                        else if (item is ClipBoard)
                        {
                            if (string.IsNullOrEmpty(temText))
                            {
                                ((ClipBoard)item).CurtText = temText;
                            }
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }

    #endregion

   public class p : IEnumerable
    {
        public IEnumerator GetEnumerator()
        {
            throw new NotImplementedException();
        }
        class pEnumer : IEnumerator
        {
            public object Current => throw new NotImplementedException();

            public bool MoveNext()
            {
                throw new NotImplementedException();
            }

            public void Reset()
            {
                throw new NotImplementedException();
            }
        }
    }



    #region 另一个例子 所有的类应该分开到不同的文件 

    //抽象中介者
    public abstract class TransferMediatorAbs
    {

        protected Dictionary<string, CardPlayerAbs> cardPlayersDic = new Dictionary<string, CardPlayerAbs>();

        public bool AddCardPlayer(CardPlayerAbs card)
        {
            if (cardPlayersDic.ContainsKey(card.CardNumber))
            {
                return false;
            }
            this.cardPlayersDic.Add(card.CardNumber, card);
            return true;
        }

        /// <summary>
        /// 转账方法 转账方向 A—>B ， 如果cardPlayersList 不包含玩家A和玩家B 或者余额不足 
        /// </summary>
        /// <param name="cardANu">玩家A</param>
        /// <param name="cardBNu">玩家B</param>
        /// <param name="money">转账钱数</param>
        /// <returns>是否返回转账成功</returns>
        public abstract bool TransferMoney(string cardANu, string cardBNu, float money);
    }

    //抽象账户类
    public abstract class CardPlayerAbs
    {
        float accountBalanceNu = 0;
        TransferMediatorAbs transfer;
        public readonly string CardNumber = string.Empty;
        public CardPlayerAbs(TransferMediatorAbs transfer, float money, string cardNu) : this(transfer, cardNu)
        {
            if (money > 0)
            {
                this.accountBalanceNu = money;
            }
        }
        public CardPlayerAbs(TransferMediatorAbs transfer, string cardNu)
        {
            this.transfer = transfer;
            this.CardNumber = cardNu;
        }

        /// <summary>
        /// 添加到中介类中
        /// </summary>
        /// <returns></returns>
        public bool AddMediatior()
        {
            //中介者保存玩家类
            return transfer.AddCardPlayer(this);
        }

        /// <summary>
        /// 账户金额增加（供自己存入金额和其他账户转入）
        /// </summary>
        /// <param name="money"></param>
        /// <returns></returns>
        public bool TransInMoney(float money)
        {
            if (money >= 0)
            {
                this.accountBalanceNu += money;
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 转出
        /// </summary>
        /// <param name="money"></param>
        /// <returns></returns>
        public bool TransOutMoney(float money, string cardNumber)
        {
            if (cardNumber == this.CardNumber)
            {
                Console.WriteLine("不能自己转给自己钱！！账号：{0}", this.CardNumber);
                return false;
            }
            return transfer.TransferMoney(this.CardNumber, cardNumber, money);
        }


        /// <summary>
        /// 账户减少金额
        /// </summary>
        /// <param name="money"></param>
        /// <returns>false表示操作失败</returns>
        public bool LessenMoney(float money)
        {
            if (money >= 0)
            {
                if (this.accountBalanceNu - money < 0)
                {
                    return false;
                }
                this.accountBalanceNu -= money;
                return true;
            }
            else
            {
                return false;
            }
        }

    }

    //具体的中介类
    public class TransferMediator : TransferMediatorAbs
    {
        public override bool TransferMoney(string cardANu, string cardBNu, float money)
        {
            //1.先判断账户是否存在
            if (this.cardPlayersDic.ContainsKey(cardANu) && this.cardPlayersDic.ContainsKey(cardBNu))
            {
                //2. 转出账户是否成功
                if (cardPlayersDic[cardANu].LessenMoney(money))
                {
                    return cardPlayersDic[cardBNu].TransInMoney(money);
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }

    //具体账户类
    public class CardPlay : CardPlayerAbs
    {
        public CardPlay(TransferMediatorAbs transfer, string cardNu) : base(transfer, cardNu)
        {
        }

        public CardPlay(TransferMediatorAbs transfer, float money, string cardNu) : base(transfer, money, cardNu)
        {
        }
    }

    #endregion
}
